Skip to main content

Design Patterns

Credits: https://refactoring.guru/design-patterns

Design patterns are typical solutions to common problems in software design. Each pattern is like a blueprint that you can customize to solve a particular design problem in your code.

Design patterns differ by their complexity, level of detail and scale of applicability to the entire system being designed. I like the analogy to road construction: you can make an intersection safer by either installing some traffic lights or building an entire multi-level interchange with underground passages for pedestrians.

The most basic and low-level patterns are often called idioms. They usually apply only to a single programming language.

The most universal and high-level patterns are architectural patterns. Developers can implement these patterns in virtually any language. Unlike other patterns, they can be used to design the architecture of an entire application.

In addition, all patterns can be categorized by their intent, or purpose. This book covers three main groups of patterns:

  • Creational patterns provide object creation mechanisms that increase flexibility and reuse of existing code.

  • Structural patterns explain how to assemble objects and classes into larger structures, while keeping these structures flexible and efficient.

  • Behavioral patterns take care of effective communication and the assignment of responsibilities between objects.

22 Design Patterns

https://refactoring.guru/design-patterns/catalog

Creational :

  1. Factory Method (Virtual Constructor) TsExample
  • Provides an interface for creating objects, but allows subclasses to alter the type of objects that will be created
  1. Abstract Method TsExample
  • Provides an interface for creating families of related objects, without specifying their concrete classes
  1. Builder TsExample
  • Separates the construction of a complex object from its representation, allowing the same construction process to create different representation
  1. Prototype (clone) TsExample
  • Creates new objects by cloning an existing object rather than creating a new instance from scratch
  1. Singleton TsExample
  • Ensures that only one instance of a class is created and provides a global point of access to that instance

Behavioral:

  1. Chain of Responsability TsExample
  • Allows multiple objects to handle a request with each object deciding whether to handle the request or pass it on to the next object
  1. Command TsExample
  • Encapsulates a request as an object, allowing the request to be parameterized with different arguments, queued or logged, and undone if necessary
  1. Iterator TsExample
  • Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation
  1. Mediator TsExample
  • Defines an object that encapsulates how a set of objects interact, promoting loose coupling between those objects.
  1. Memento TsExample
  • Provides the ability to restore an object to its previous state (undo)
  1. Observer TsExample
  • Defines a one-to-many dependency between objects, so that when one object changes states, all its dependents are notified and updated automatically
  1. State TsExample
  • Allow an object to alter its behavior when its internal state changes
  1. Strategy TsExample

-Defines a family of algorithms , encapsulates each one and makes them interchangeable. It lets the algorithm vary independently from clients that use it

  1. Template Method TsExample
  • Defines a skeleton of an algorithm in a base class allowing subclasses to provide specific behavior for some of the steps
  1. Visitor TsExample
  • Separates an algorithm from an object structure on which it operates. The algorithm can then be changed without changing the object structure

Structural:

  1. Adapter (Wrapper) TsExample
  • Converts the interface of a class into another interface that clients expect. It allows incompatible class to work together
  1. Bridge TsExample
  • Decouples an abstraction from its implementation so that two can vary independently
  1. Composite TsExample
  • Composes objects into tree structures to represent whole-part hierarchies. It lets clients treat individual objects as compositions of objects uniformly
  1. Decorator TsExample
  • Attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
  1. Facade TsExample
  • Provides a simplified interface to a complex subsystem, making it easier to use and understand.
  1. Flyweigth TsExample
  • Shares objects to reduce memory usage. It is useful when many identical object must be created
  1. Proxy TsExample
  • Provides a surrogate or placeholder for another object to control access to it

My studies on Design Patterns :

https://github.com/luizrosalba/OO